package com.ruoyi.project.system.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.dto.ResWithMedicinesDTO;
import com.ruoyi.project.system.service.IResMedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 预约+药品关联查询接口
 */
@RestController
@RequestMapping("/api/resMed") // 接口前缀，可根据项目规范调整
public class ResMedController extends BaseController {

    @Autowired
    private IResMedService resMedService;

    /**
     *  GET请求：查询所有resolve=1的预约记录及关联药品
     *  接口地址：http://localhost:8080/api/resMed/getResWithMedicines
     */
    @GetMapping("/getResWithMedicines")
    public AjaxResult getResWithMedicines() {
        try {
            List<ResWithMedicinesDTO> result = resMedService.getResWithMedicines();
            // 用RuoYi自带的AjaxResult返回（统一响应格式）
            return AjaxResult.success("查询成功", result);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
    
    /**
     * POST请求：处理药品下发，减少对应药品库存
     * 接口地址：http://localhost:8080/api/resMed/process
     */
    @PostMapping("/process")
    public AjaxResult processMedicineDistribution(@RequestParam String resId) {
        try {
            boolean result = resMedService.processMedicineDistribution(resId);
            if (result) {
                return AjaxResult.success("药品下发处理成功");
            } else {
                return AjaxResult.error("药品下发处理失败：未找到相关记录");
            }
        } catch (Exception e) {
            return AjaxResult.error("药品下发处理失败：" + e.getMessage());
        }
    }
}